// @flow
import React, { Component } from 'react'
import styles from './DeviceGrid.css'
import { AgGridReact } from 'ag-grid-react'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import { TableResize } from 'ag-grid-addons'
import Pagination from 'components/Pagination'

type Props = {
  columnDefs: Array<Object>,
  pageSize: number,
  current: number,
  total: number,
  rowData: Array<Object>,
  getData: Function
}
type States = {
  rowData: Array<Object>,
  total: number,
  current: number
}
class DeviceGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  onPageChange: Function
  api: Object
  TableResize: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      rowData: [],
      total: 1,
      current: 1
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      pagination: false,
      // animateRows: true,
      rowSelection: 'multiple',
      enableColResize: true,
      // groupSelectsChildren: true,
      // suppressCopyRowsToClipboard: true,
      icons: {
        groupExpanded: '<i class="fa fa-minus-square-o" aria-hidden="true"></i>',
        groupContracted: '<i class="fa fa-plus-square-o" aria-hidden="true"></i>'
      },
      onModelUpdated: () => {
        if (this.api) {
          this.api.sizeColumnsToFit()
          this.TableResize()
        }
      },
      getNodeChildDetails: (node: Object) => {
        // console.log(node)
        if (node.children.length > 0) {
          return {
            group: true,
            children: node.children
          }
        } else {
          return null
        }
      }
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.onPageChange = this.onPageChange.bind(this)
    this.TableResize = TableResize.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }
  componentWillReceiveProps (nextProps: Object) {
    this.setState({
      total: nextProps.total,
      current: nextProps.current,
      rowData: nextProps.rowData
    })
  }
  onPageChange (page: number, pageSize: number) {
    this.props.getData(page, pageSize)
  }

  render () {
    const { current, total } = this.state
    const { pageSize } = this.props
    return (
      <div className={styles['grid'] + ' ag-fresh'}>
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: this.props.columnDefs,
          rowData: this.state.rowData,
          onGridReady: this.onGridReady
        }}
          />
        <Pagination
          total={total}
          current={current} pageSize={pageSize} onChange={this.onPageChange} />
      </div>
    )
  }
}

export default DeviceGrid
